package edu.taru.view;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import edu.taru.pojo.User;
import edu.taru.service.*;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.awt.event.ActionEvent;

public class FindView extends JFrame{
	private JTextField textField;
	private JTable table;
	String [] header= {"编号","用户名","密码","性别","城市","邮箱","出生日期","姓名","部门编号","部门名称"};
	String[][] data =new String[10][10];
	public FindView() {
		setVisible(true);
		setTitle("用户列表");
		setSize(774,458);
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u6570\u636E", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(0, 0, 557, 108);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(117, 53, 123, 21);
		panel.add(textField);
		textField.setColumns(10);
		/**
		 * 查找
		 */
		JButton button = new JButton("查找");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				/**
				 * 查询用户
				 */
				String username = textField.getText();
				Services services = new Services();
				if(username != null) {
					try{
							Vector<Vector<String>> vectors = services.search(username);
							Vector   new_header =new Vector();
							for(int i =0;i<header.length;i++){
								new_header.add(header[i]);
						}
						DefaultTableModel tablemodel =new  DefaultTableModel(vectors,new_header);
						table.setModel(tablemodel);
						table.updateUI();
					}catch(Exception ex){
						//TODO
					}
				}else {
					JOptionPane.showMessageDialog(FindView.this, "请输入用户名");
				}
			}
		});
		button.setBounds(285, 52, 93, 23);
		panel.add(button);

		JButton button_1 = new JButton("刷新");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/**
				 * 刷新出全部用户
				 */
				Services services = new Services();
				try{
					Vector<Vector<String>> vectors = services.refresh();
					Vector   new_header =new Vector();
					for(int i =0;i<header.length;i++){
						new_header.add(header[i]);
					}
					
					DefaultTableModel tablemodel =new  DefaultTableModel(vectors,new_header);
					table.setModel(tablemodel);
					table.updateUI();
					
				}catch(Exception ex){
				}
			
			}
		});
		button_1.setBounds(392, 52, 93, 23);
		panel.add(button_1);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 128, 738, 187);
		getContentPane().add(scrollPane);
		
		table = new JTable(data,header);
		scrollPane.setViewportView(table);
		
		JButton button_2 = new JButton("删除用户");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					//被选中的行
					int index = table.getSelectedRow();
					//获取选中的行和第
					String id = table.getValueAt(index,0).toString();
					System.out.println(id);
					Services services = new Services();
					services.delete(id);
					JOptionPane.showMessageDialog(FindView.this,"删除成功，请刷新页面");
				}catch(Exception ex) {
					JOptionPane.showMessageDialog(FindView.this,"至少选择一行");
				}
			}
		});
		button_2.setBounds(79, 345, 93, 23);
		getContentPane().add(button_2);
		
		JButton button_3 = new JButton("修改用户");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/**
				 * 修改用户
				 */
				try {
					//被选中的行
					int selectedRow = table.getSelectedRow();
					//获取选中的行和第一列的值
					String id = table.getValueAt(selectedRow,0).toString();
					Services services = new Services();
					User user = services.update1(id);
					UpdateView updateView = new UpdateView(user);
				}catch(Exception ex) {
					JOptionPane.showMessageDialog(FindView.this,"至少选择一行");
				}
			}
		});
		button_3.setBounds(370, 345, 93, 23);
		getContentPane().add(button_3);
	}
}
